home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / AnalogClockView.BackModule / AnalogClockView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.4 KB  |  152 lines

  1. //
  2. // AnalogClockView.m
  3. //
  4. // Matt Pharr- pharr@cs.yale.edu
  5. //
  6.  
  7. #import "AnalogClockView.h"
  8. #import "AnalogClockWraps.h"
  9. #import "AnalogClockWraps.c"
  10. #import <appkit/graphics.h>
  11. #import <libc.h>
  12. #import <dpsclient/wraps.h>
  13. #import <time.h>
  14. #import <math.h>
  15. #import <strings.h>
  16. #import <appkit/NXImage.h>
  17. #import <appkit/publicWraps.h>
  18.  
  19.  
  20. @implementation AnalogClockView
  21.  
  22. - fixPosition
  23. {
  24.   if (currentLocation.x + currentSize.width >= bounds.size.width)
  25.     currentLocation.x = bounds.size.width - currentSize.width;
  26.   if (currentLocation.x <= bounds.origin.x)
  27.     currentLocation.x = 0;
  28.  
  29.   if (currentLocation.y + currentSize.height >= bounds.size.height)
  30.     currentLocation.y = bounds.size.height - currentSize.height;
  31.   if (currentLocation.y <= bounds.origin.y)
  32.     currentLocation.y = 0;
  33.  
  34.   return self;
  35. }
  36.  
  37.  
  38. - bounceIfNeeded
  39. {
  40.   if (currentLocation.x + currentSize.width >= bounds.size.width)
  41.     moveVector.x= (-1) * randBetween(0.5, 1.5);
  42.   if (currentLocation.x <= bounds.origin.x)  // keep it ON the screen
  43.     moveVector.x= randBetween(0.5, 1.5);
  44.  
  45.   if (currentLocation.y + currentSize.height >= bounds.size.height)
  46.     moveVector.y= (-1) * randBetween(0.5, 1.5);
  47.   if (currentLocation.y <= bounds.origin.y)
  48.     moveVector.y= randBetween(0.5, 1.5);
  49.  
  50.   return self;
  51. }
  52.  
  53.  
  54. - oneStep
  55. {
  56.   time_t tTime;
  57.   struct tm *currentTime;
  58.   NXPoint p={ 0, CLOCK_HEIGHT+24};
  59.  
  60.   currentLocation.x += moveVector.x;       // move the x and y positions of the image
  61.   currentLocation.y += moveVector.y;       // as indicated by the move vectors
  62.   [self bounceIfNeeded];
  63.   [self fixPosition];
  64.  
  65.   time(&tTime);
  66.   currentTime= localtime(&tTime);
  67.  
  68.   if (secs != currentTime->tm_sec) {
  69.     if (mins != currentTime->tm_min) {
  70.       if ([hourMinuteImage lockFocus] == YES) {
  71.     hours= currentTime->tm_hour;
  72.     mins= currentTime->tm_min;
  73.     drawHourMinuteFace(hours % 12, mins, CLOCK_WIDTH, CLOCK_HEIGHT, hasColor);
  74.     [hourMinuteImage unlockFocus];
  75.       }
  76.     }
  77.  
  78.     secs= currentTime->tm_sec;
  79.  
  80.     currentLocation.x += moveVector.x;   // move it one more time so it's not so jerky
  81.     currentLocation.y += moveVector.y;   // when the time changes...
  82.     [self bounceIfNeeded];
  83.     [self fixPosition];
  84.  
  85.     if ([currentImage lockFocus] == YES) {
  86.       [hourMinuteImage composite:NX_COPY toPoint:&p];
  87.       drawSecond(secs, CLOCK_WIDTH, CLOCK_HEIGHT, hasColor);
  88.       [currentImage unlockFocus];
  89.     }
  90.   }
  91.  
  92.   [currentImage composite:NX_COPY toPoint:¤tLocation];
  93.  
  94.   usleep((1*1000000)/68);                 // sleep a few vblanks
  95.  
  96.   return self;
  97. }
  98.  
  99.  
  100.  
  101. - (const char *)windowTitle
  102. {
  103.   return "Analog Clock";
  104. }
  105.  
  106.  
  107.  
  108. - drawSelf:(const NXRect *)rects :(int)rectCount
  109. {
  110.   if (!rects || !rectCount) {
  111.     return self;
  112.   }
  113.  
  114.   PSsetgray(0);
  115.   NXRectFill(rects);
  116.  
  117.   return self;
  118. }
  119.  
  120.  
  121. - initFrame:(const NXRect *)frameRect
  122. {
  123.   [super initFrame:frameRect];
  124.  
  125.   currentLocation.x= 400.0;
  126.   currentLocation.y= 400.0;
  127.  
  128.   moveVector.x= randBetween(0.5, 1.5);
  129.   moveVector.y= randBetween(0.5, 1.5);
  130.  
  131.   currentSize.width= CLOCK_WIDTH + 24;
  132.   currentSize.height= CLOCK_HEIGHT + 24;
  133.  
  134.   currentImage= [[NXImage alloc] initSize:¤tSize];
  135.   [currentImage setFlipped:YES];
  136.  
  137.   hourMinuteImage= [[NXImage alloc] initSize:¤tSize];
  138.   [hourMinuteImage setFlipped:YES];
  139.  
  140.   secs= -1;    // force it to redraw the bitmap the first time through
  141.   mins= -1;
  142.  
  143.   if ([Window defaultDepthLimit] == NX_TwoBitGrayDepth) {   // monochrome screen?
  144.     hasColor= 0;
  145.   }
  146.   else hasColor= 1;
  147.  
  148.   return self;
  149. }
  150.  
  151. @end
  152.